package com.robot.clientfactory;

import com.robot.dto.RunTestContext;
import com.robot.model.BuildTestCaseDo;
import com.robot.model.Protocol;
import com.robot.service.DubboService;
import com.robot.util.DateUtil;
import com.robot.util.ReportLogUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * User: li
 * Date: 2019/12/1
 * Time: 10:18
 * PROJECT：robot
 * PACKAGE: com.robot
 *
 * @author:55324936@qq.com To change this template use File | Settings | File Templates.
 */
@Slf4j
@Component
public class DubboProcessor implements ClientProcessor {

    @Autowired
    DubboService dubboService;
    @Autowired
    DateUtil dateUtil;
    @Override
    public void runTest(RunTestContext runTestContext) {
        Protocol protocol=new Protocol();
        BeanUtils.copyProperties(runTestContext.getBuildTestInterfaceDo(),protocol);
        BuildTestCaseDo buildTestCaseDo =runTestContext.getBuildTestCaseDo();
        protocol.setParam(buildTestCaseDo.getParam());
        ReportLogUtil reportLogUtil =runTestContext.getReportLogUtil();
        try {
            buildTestCaseDo.setStartTime(dateUtil.getCurrentTime());
            Long startTime = System.currentTimeMillis();
            String actualResult = dubboService.invoke(protocol);
            Long endTime = System.currentTimeMillis();
            Long runTime = (endTime-startTime)/1000;
            buildTestCaseDo.setRunTime(runTime.toString());
            buildTestCaseDo.setActualResult(actualResult);
        }catch (Exception e){
            log.error("执行dubbo接口异常",e);
            reportLogUtil.writeRedSpan("dubbo接口执行异常,"+e.getMessage());
        }
    }
}
